package com.hall.service;

import com.hall.dao.MeetDetailsMapper;
import com.hall.pojo.*;
import com.hall.pojo.common.Result;
import com.hall.utils.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class MeetDetailsServiceImpl implements MeetDetailsService {
    @Autowired
    MeetDetailsMapper meetdemapper;
    @Override
    public Result selectAppById(String id) {
        Appointment ap = meetdemapper.selectAppById(id);
        return ap==null ? Result.error("无符合的会诊"): Result.success(ap,"请求成功");
    }

    @Override
    public Result<List<AppointmentUser>> selectAppUserById(String id) {
        if("".equals(id)||id.isEmpty()) return Result.error("无效的会议id");
        List<AppointmentUser> au = meetdemapper.selectAppUserById(id);
        return au==null? Result.error("无符合的数据"):Result.success(au,"请求成功");
    }

    @Override
    public Result addLike(AppointmentClick ac, HttpSession session,HttpServletRequest request) {
        String id = UserSession.getSysUser(request).getId();
        if (ac.getUserId()==null ||"".equals(ac.getUserId())){
            if(id==null){
                return Result.error("无效的用户名,请重试");
            }
            ac.setUserId(id);
        }
        //新增前先检查session中是否有该次会议的点赞缓存
        String info = (String) session.getAttribute("like"+ac.getAppointmentId()+ac.getUserId());
        if(info!=null){
            return Result.success(null,"该会诊你已点过赞");//如果缓存中有,不执行数据库查询(避免多次访问的数据库操作)
        }
        //如果缓存没有,执行数据库查询
        int lk = meetdemapper.selectLike(ac);
        if(lk>0){
            return Result.success(null,"该会诊你已点过赞");
        }else{ //如果数据库也没有,执行新增
            meetdemapper.addLike(ac); //执行新增后,将当前会诊数据缓存到session
            session.setAttribute("like"+ac.getAppointmentId()+ac.getUserId(),"ok");
            return Result.success(null,"点赞成功");
        }
    }

    @Override
    public Result addWatch(AppointmentWatch aw,HttpSession session,HttpServletRequest request) {
        String id = UserSession.getSysUser(request).getId();
        if (aw.getUserId()==null ||"".equals(aw.getUserId()) || "undefined".equals(aw.getUserId())){
            if(id==null){
                return Result.error("无效的用户名,请重试");
            }
            aw.setUserId(id);
        }
        //新增前先检查session中是否有该次会议的访问缓存
        String wt = (String) session.getAttribute("watch"+aw.getAppointmentId()+aw.getUserId());
        if(wt!=null){
            return Result.success(null,"已浏览过");//如果缓存中有,不执行数据库查询(避免多次访问的数据库操作)
        }
        //如果缓存没有,执行数据库查询
        int lk = meetdemapper.selectWatch(aw);
        if(lk>0){
            return Result.success(null,"已浏览过");
        }else{ //如果数据库也没有,执行新增
            meetdemapper.addWatch(aw); //执行新增后,将当前会诊数据缓存到session
            session.setAttribute("watch"+aw.getAppointmentId()+aw.getUserId(),"ok");
            return Result.success(null,"浏览量加1");
        }
    }

    @Override
    public Result getWatchNum(String id) {
        int i =meetdemapper.selectWatchNum(id);
        return i>0?Result.success(i,"请求成功"):Result.error("请求失败");
    }

    public void setMeetDetailsMapper(MeetDetailsMapper meetDetailsMapper) {
    }
}
